// OpenCallback.h

#ifndef __OPENCALLBACK_H
#define __OPENCALLBACK_H


#include "Common/FileSystem/FileFind.h"

#include "Common/COM/IPassword.h"

#include "Common/Archive/IArchive.h"

#ifdef _SFX
#include "ProgressDialog.h"
#else
#include "ProgressDialog2.h"
#endif


class ATL_NO_VTABLE COpenArchiveCallback :
	public CComObjectRootEx<CComSingleThreadModel>,
  public IArchiveOpenCallback,
  public IArchiveOpenVolumeCallback,
  public IArchiveOpenSetSubArchiveName,
  public IProgress,
  public ICryptoGetTextPassword 
{
  CString _folderPrefix;
  Common::FileSystem::File::CFileInfo _fileInfo;
  CComAutoCriticalSection _criticalSection;
  bool _subArchiveMode;
  CString _subArchiveName;

public:
	BEGIN_COM_MAP(COpenArchiveCallback)
		COM_INTERFACE_ENTRY(IArchiveOpenCallback)
		COM_INTERFACE_ENTRY(IArchiveOpenVolumeCallback)
		COM_INTERFACE_ENTRY(IArchiveOpenSetSubArchiveName)
		COM_INTERFACE_ENTRY(IProgress)
		COM_INTERFACE_ENTRY(ICryptoGetTextPassword)
	END_COM_MAP()


	DECLARE_PROTECT_FINAL_CONSTRUCT()

	HRESULT FinalConstruct()
	{
		ParentWindow = 0;
  
		_subArchiveMode = false;
		PasswordIsDefined = false;
		PasswordWasAsked = false;
		return S_OK;
	}

	void FinalRelease()
	{
	}

  bool PasswordIsDefined;
  bool PasswordWasAsked;
  CString Password;
  HWND ParentWindow;
  CProgressDialog2 ProgressDialog;

  
  //INTERFACE IProgress
  STDMETHOD(SetTotal)(UINT64 total); 
  STDMETHOD(SetCompleted)(const UINT64 *completeValue);
  
  //INTERFACE_IArchiveOpenCallback
  STDMETHOD(SetTotal)(const UINT64 *files, const UINT64 *bytes); 
  STDMETHOD(SetCompleted)(const UINT64 *files, const UINT64 *bytes);

  //INTERFACE IArchiveOpenVolumeCallback
  STDMETHOD(GetProperty)(PROPID propID, PROPVARIANT *value) ; 
  STDMETHOD(GetStream)(const wchar_t *name, IInStream **inStream) ;

  // ICryptoGetTextPassword
  STDMETHOD(CryptoGetTextPassword)(BSTR *password);

  STDMETHOD(SetSubArchiveName(const wchar_t *name))
  {
    _subArchiveMode = true;
    _subArchiveName = name;
    return  S_OK;
  }

  void LoadFileInfo(const CString &folderPrefix, const CString &fileName)
  {
    _folderPrefix = folderPrefix;
    if (!_fileInfo.Find(_folderPrefix + fileName))
      throw 1;
  }
  void ShowMessage(const UINT64 *completed);

  INT_PTR StartProgressDialogAndWait(const CString &title, HANDLE hEvent)
  {
	  return ProgressDialog.DoModalAndWait(title, hEvent, ParentWindow);
  }
};

#endif
